package com.itita.weibo.ui;

import java.util.HashMap;
import java.util.List;

import weibo4andriod.Comment;
import weibo4andriod.Status;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.ListCommentAdapter;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;

 

public class ListCommentsActivity extends Activity implements IWeiboActivity, OnClickListener{
    
	//返回
	public static final int COME_BACK = 1;
	
	//发布评论
	public static final int POST_COMMENT = 2;
	
	private Button btnBack;
	
	private Button  btnComment;
	
	//查看的该微博的评论
	private long statusId;
	
	private ListView listView;
	
	private View process;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//取消标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 
		 
		HashMap hm=new HashMap();
		Task tsListComment=new Task(Task.TASK_GET_WEIBO_COMMENT_LIST,hm);
	    MainService.newTaskToTaskQueneHead(tsListComment);
 
		setContentView(R.layout.lsit_comments);
		this.initControlsAndAddControlsEvent();
		MainService.addActivity(this);
	}
	@Override
	protected void onResume() {
		super.onResume();
		init();
	}
	public void init() {
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
	    statusId = bundle.getLong("statusId");
		if(statusId == 0){
			WeiboUtil.myToastShow(this, "暂无数据");
		}else{
			HashMap hm=new HashMap();
			hm.put("statusId", statusId);
			Task tsListComment=new Task(Task.TASK_GET_WEIBO_COMMENT_LIST,hm);
		    MainService.newTaskToTaskQueneHead(tsListComment);
		}
		
	}

	public void refresh(Object... param) {
		this.process.setVisibility(View.GONE);
		if(param[1] == null ){
			if(!MainService.isVerifySucc){
				WeiboUtil.myToastShow(this, "网络连接失败");
			}else{
				WeiboUtil.myToastShow(this, "暂无数据");
			}
			
			return;
		}
		this.listView.setAdapter(new ListCommentAdapter(this, (List<Comment>)param[1]));
	}
   
	/**
	 * 事件的处理
	 * @param v 视图(View)
	 */
	public void onClick(View v) {
		 int tag = (Integer) v.getTag();
		 switch (tag) {
			case ListCommentsActivity.COME_BACK:
				this.finish();
				break;
				
			case ListCommentsActivity.POST_COMMENT:
				Intent intent = new Intent();
				intent.setClass(this, CommentActivity.class);
				Bundle bundle = new Bundle();
				bundle.putLong("statusId", statusId);
				intent.putExtras(bundle);
				startActivity(intent);
				break;
			default:
				break;
		}
	}


	@Override
	public void initControlsAndAddControlsEvent() {
		View titleview=this.findViewById(R.id.list_comment_title);
		this.btnBack = (Button) titleview.findViewById(R.id.title_bt_left);
	    //返回
//		this.btnBack.setText(R.string.imageviewer_back);
		this.btnBack.setBackgroundResource(R.drawable.title_back);
		
		//评论
		this.btnComment = (Button) titleview.findViewById(R.id.title_bt_right);
		this.btnComment.setBackgroundResource(R.drawable.title_post);
		
		this.btnBack.setTag(COME_BACK);
		this.btnComment.setTag(POST_COMMENT);
		
		
		TextView tvTitle = ((TextView) titleview.findViewById(R.id.textView));
		tvTitle.setText(R.string.title_btn_comment); 
//		
//		final ImageView imageViewTitle = (ImageView) titleview.findViewById(R.id.imageViewTitle);
//    	imageViewTitle.setImageResource(R.drawable.comments);
    	
		
		//为返回和发布添加事件
		this.btnBack.setOnClickListener(this);
		this.btnComment.setOnClickListener(this);
		
		this.listView = (ListView) this.findViewById(R.id.comment_listview);
		  //获取列表对象和进度条对象
	    process = this.findViewById(R.id.progress4ListComment);
	    
	    process.setVisibility(View.VISIBLE);
	}

}
